package com.moefor.clover.init.config;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:config/DBConfig.properties")
public class DataSourceConfig {
	
	@Autowired
    private Environment env;

	@Bean(name = "dataSource", destroyMethod = "close")
    public DataSource dataSource() {
		DataSource dataSource = new DataSource();
		
	  	dataSource.setUrl(env.getProperty("jdbc.url"));
    	dataSource.setUsername(env.getProperty("jdbc.username"));
    	dataSource.setPassword(env.getProperty("jdbc.password"));
    	dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
    	
    	dataSource.setInitialSize(Integer.valueOf(env.getProperty("jdbc.initialSize")));
    	dataSource.setMinIdle(Integer.valueOf(env.getProperty("jdbc.minIdle")));
    	dataSource.setMaxActive(Integer.valueOf(env.getProperty("jdbc.maxActive")));
    	dataSource.setMaxIdle(Integer.valueOf(env.getProperty("jdbc.maxIdle")));
    	dataSource.setMaxWait(Integer.valueOf(env.getProperty("jdbc.maxWait")));
    	
    	dataSource.setMinEvictableIdleTimeMillis(Integer.valueOf(env.getProperty("jdbc.minEvictableIdleTimeMillis")));
    	dataSource.setTimeBetweenEvictionRunsMillis(Integer.valueOf(env.getProperty("jdbc.timeBetweenEvictionRunsMillis")));
    	dataSource.setValidationQuery(env.getProperty("jdbc.validationQuery"));
    	dataSource.setValidationInterval(Long.valueOf(env.getProperty("jdbc.validationInterval")));
    	dataSource.setTestWhileIdle(Boolean.valueOf(env.getProperty("jdbc.testWhileIdle")));
    	
    	dataSource.setJdbcInterceptors(env.getProperty("jdbc.jdbcInterceptors"));
    	
    	dataSource.setDefaultAutoCommit(Boolean.valueOf(env.getProperty("jdbc.defaultAutoCommit")));
    	return dataSource;
	}
}
